package com.yjyw.tiexie.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;


import androidx.annotation.NonNull;

import com.yjyw.tiexie.common.BaseActivity;
import com.yjyw.tiexie.common.permission.PermissionCallback;
import com.yjyw.tiexie.common.permission.PermissionManager;

import java.util.List;

/**
 * Created by SmallYou on 2018/12/3 0003.
 * DESC:TODO()
 */

public abstract class CheckPermissionActivity extends BaseActivity implements PermissionCallback {

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    //父类已经实现接口方法，子类不需要实现，同意不需要做任何事情
    @Override
    public void onPermissionGranted(int requestCode, List<String> grant) {
//        Toast.makeText(this, "权限通过", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onPermissionDenied(int requestCode, List<String> denied) {
//        Toast.makeText(this, "权限拒绝", Toast.LENGTH_LONG).show();
        //用户点击拒绝 有可能点击不再询问
        if (PermissionManager.somePermissionNotAskAgain(this, denied)) {
//            Toast.makeText(this, "有权限点击了不再询问", Toast.LENGTH_LONG).show();
            Log.e("NotAskAgain", "somePermissionNotAskAgain");
            showMissingPermissionDialog();
        } else {
        }
    }


    /**
     * 提示用户的 dialog
     */
    protected void showMissingPermissionDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("当前应用缺少权限。会导致部分功能无法使用！\n\n请点击\"设置\"-\"权限\"-打开所需权限。");
        // 拒绝, 退出应用
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i("info", "8--权限被拒绝,此时不会再回调onRequestPermissionsResult方法");
                    }
                });
        builder.setPositiveButton("设置",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i("info", "4,需要用户手动设置，开启当前app设置界面");
                        startAppSettings();
                    }
                });
        builder.setCancelable(false);
        builder.show();
    }

    /**
     * 打开 App设置界面
     */
    private void startAppSettings() {
        Intent intent = new Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //从设置回来

    }


}
